﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBC_Quiz_Show.Bussiness;

namespace KBC_Quiz_Show.Presentation
{
    public partial class FrmManager : DevComponents.DotNetBar.Office2007RibbonForm
    {
        DataSet ds1, dsquestion;
        QuestionController quescontroller = new QuestionController();
        public FrmManager()
        {
            InitializeComponent();
        }
        public void ResetForm()
        {
            txtQuestionContent.Text = "";
            txtAnsA.Text = "";
            txtAnsB.Text = "";
            txtAnsC.Text = "";
            txtAnsD.Text = "";
            cboAnsCorrect.Text = "";
            cboLevel.Text = "";
            cboQuesID.Text = "";
        }
        public void LoadForm()
        {
            //ds = quescontroller.LoadQuestion();
            // dataGridViewX1.DataSource = ds.Tables[0];
            ds1 = quescontroller.LoadQuestion();
            dataGridViewX1.DataSource = ds1.Tables[0];
        }

        public void InsertQuestion()
        {
            //int quesID = (int)(cboQuesID.SelectedValue);
            //int quesID = Int32.Parse(idq.Text.ToString());
            String quesContent = txtQuestionContent.Text;
            String ans1 = txtAnsA.Text;
            String ans2 = txtAnsB.Text;
            String ans3 = txtAnsC.Text;
            String ans4 = txtAnsD.Text;
            int level = Int32.Parse(cboLevel.SelectedItem.ToString());

            String anstrue = (String)cboAnsCorrect.SelectedItem;

            quescontroller.InsertQuestion(quesContent, ans1, ans2, ans3, ans4, level, anstrue);
            MessageBox.Show("Insert successful !!!");


        }

        private void FrmManager_Load(object sender, EventArgs e)
        {
            cboLevel.Items.Add("1");
            cboLevel.Items.Add("2");
            cboLevel.Items.Add("3");
            cboLevel.Items.Add("4");
            cboLevel.Items.Add("5");
            cboLevel.Items.Add("6");
            cboLevel.Items.Add("7");
            cboLevel.Items.Add("8");
            cboLevel.Items.Add("9");
            cboLevel.Items.Add("10");
            cboLevel.Items.Add("11");
            cboLevel.Items.Add("12");
            cboLevel.Items.Add("13");
            cboAnsCorrect.Items.Add("A");
            cboAnsCorrect.Items.Add("B");
            cboAnsCorrect.Items.Add("C");
            cboAnsCorrect.Items.Add("D");


            dsquestion = quescontroller.LoadQuestion();
            cboQuesID.DataSource = dsquestion.Tables[0];
            cboQuesID.DisplayMember = dsquestion.Tables[0].Columns[0].ToString();
            cboQuesID.ValueMember = dsquestion.Tables[0].Columns[0].ToString();
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            // cboQuesID.Items
            this.LoadForm();
        }
        public void UpdateQuestion()
        {
            //(int quesID, string quesContent, string ans1, string ans2, string ans3, string ans4, int level, string anstrue)
            int id = Int32.Parse(txtidq.Text.ToString());
            String quesContent = txtQuestionContent.Text;
            String ans1 = txtAnsA.Text;
            String ans2 = txtAnsB.Text;
            String ans3 = txtAnsC.Text;
            String ans4 = txtAnsD.Text;
            int level = Int32.Parse(cboLevel.SelectedItem.ToString());
            String anstrue = cboAnsCorrect.SelectedItem.ToString();

            quescontroller.UpdateQuestion(id, quesContent, ans1, ans2, ans3, ans4, level, anstrue);
            MessageBox.Show("Update successful !!!");
            this.LoadForm();
            this.ResetForm();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.InsertQuestion();
            this.LoadForm();
            this.ResetForm();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.UpdateQuestion();
            this.LoadForm();
            this.ResetForm();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            {
                if (MessageBox.Show("You really want to delete?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        int id = Int32.Parse(txtidq.Text.ToString());
                        quescontroller.DelQuestion(id);
                        MessageBox.Show("Del successfull", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadForm();
                        ResetForm();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Errors");
                    }
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int questId = Int32.Parse(cboQuesID.Text);
            ds1 = quescontroller.SearchQuestion(questId);
            dataGridViewX1.DataSource = ds1.Tables[0];
        }

        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridViewX1.Rows[e.RowIndex];
            txtidq.Text = row.Cells[0].Value.ToString();
            txtQuestionContent.Text = row.Cells[1].Value.ToString();
            txtAnsA.Text = row.Cells[2].Value.ToString();
            txtAnsB.Text = row.Cells[3].Value.ToString();
            txtAnsC.Text = row.Cells[4].Value.ToString();
            txtAnsD.Text = row.Cells[5].Value.ToString();
            cboLevel.SelectedItem = row.Cells[6].Value.ToString();

            cboAnsCorrect.SelectedItem = row.Cells[7].Value.ToString();
            txtidq.Enabled = false;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
